//#include "lv_port_disp.h"
//#include "lv_port_fs.h"
//#include "ff.h"

//#define IMG_SDRAM_ADDR          (SDRAMM_BASE + (1*1024*1024))
//#define MAX_SIZE_COPY_FILE      (4*1024)

//static FATFS sd_fs, flash_fs;
//static uint8_t img_index = 0;

//typedef enum
//{
//	bg_1 = 0,
//	bg_2,
//	bg_3,
//	p2_1,
//	p2_2,
//	p2_3,
//	p2_4,
//	p3_eco_off,
//	p3_eco_on,
//	p4_mkwater,
//	p5_bluetea,
//	p5_flowertea,
//	p5_honey,
//	p5_milk,
//	p5_redtea,
//	p5_wolfberrytea,
//	p7_filter1,
//	p7_filter1_rst,
//	p7_filter2,
//	p7_filter2_rst,
//	p7_filter3,
//	p7_filter3_rst,
//	p8_off,
//	p8_on,
//	p10_warn1,
//	p10_warn2,
//	p11_stop,
//	p12_wateraway,
//	MAX_IMG_NUM,
//}ico_index_t;

////img list
//static const char* gui_img_name[MAX_IMG_NUM] = 
//{
//	[bg_1] = "bg_1.bin",
//	[bg_2] = "bg_2.bin",
//	[bg_3] = "bg_3.bin",
//	[p2_1] = "P2_1.bin",
//	[p2_2] = "P2_2.bin",
//	[p2_3] = "P2_3.bin",
//	[p2_4] = "P2_4.bin",
//	[p3_eco_off] = "P3_eco_off.bin",
//	[p3_eco_on] = "P3_eco_on.bin",
//	[p4_mkwater] = "p4_mkwater.bin",
//	[p5_bluetea] = "p5_bluetea.bin",
//	[p5_flowertea] = "p5_flowertea.bin",
//	[p5_honey] = "p5_honey.bin",
//	[p5_milk] = "p5_milk.bin",
//	[p5_redtea] = "p5_redtea.bin",
//	[p5_wolfberrytea] = "p5_wolfberrytea.bin",
//	[p7_filter1] = "p7_filter1.bin",
//	[p7_filter1_rst] = "p7_filter1rst.bin",
//	[p7_filter2] = "p7_filter2.bin",
//	[p7_filter2_rst] = "p7_filter2rst.bin",
//	[p7_filter3] = "p7_filter3.bin",
//	[p7_filter3_rst] = "p7_filter3rst.bin",
//	[p8_off] = "p8_off.bin",
//	[p8_on] = "p8_on.bin",
//	[p10_warn1] = "p10_warn1.bin",
//	[p10_warn2] = "p10_warn2.bin",
//	[p11_stop] = "p11_stop.bin",
//	[p12_wateraway] = "p12_wateraway.bin",
//};


//static int exf_getfree(const TCHAR *path, uint32_t *total,uint32_t *free)
//{
//    FATFS *fs1;
//    int res;
//    DWORD fre_clust=0, fre_sect=0, tot_sect=0;

//    res = f_getfree(path, &fre_clust, &fs1);
//    if(res==0)
//    {
//        tot_sect=(fs1->n_fatent-2)*fs1->csize;
//        fre_sect=fre_clust*fs1->csize;
//#if FF_MAX_SS != 512
//        tot_sect*=fs1->ssize/512;
//        fre_sect*=fs1->ssize/512;
//#endif
//        *total=tot_sect>>1;
//        *free=fre_sect>>1;
//     }
//    return res;
//}

//static int img_copy_sd_to_flash(const char *src_path, const char *dest_path)
//{
//	static uint8_t copy_status = 0;
//	static uint32_t remain_file_size = 0, file_size = 0;
//	static lv_fs_file_t sd_file, flash_file;
//	static uint32_t offset = 0;
//	char file_path[64] = {0};
//	
//	uint32_t fwr_rd_num;
//	BYTE copy_buffer[MAX_SIZE_COPY_FILE] = {0};
//	lv_fs_res_t ret;
//	
//    if(copy_status != 1)
//	{
//		sprintf(file_path, IMG_SD_PATH "%s", src_path);
//		ret = lv_fs_open(&sd_file, file_path, LV_FS_MODE_RD);
//		if(ret != LV_FS_RES_OK)
//		{
//			HXLOG("open source file fail");
//			return -1;
//		}
//		sprintf(file_path, IMG_FLASH_PATH "%s", dest_path);
//		ret = lv_fs_open(&flash_file, file_path, LV_FS_MODE_WR );
//		if(ret != LV_FS_RES_OK)
//		{
//			HXLOG("open or create file fail");
//			return -1;
//		}
//		
//		ret = (uint32_t)lv_fs_size(&sd_file, &file_size);
//		if(ret != LV_FS_RES_OK)
//		{
//			HXLOG("get file size fail");
//			return -1;
//		}
//		remain_file_size = file_size;
//		offset = 0;
//		copy_status = 1;
//		
//	}
//		
//	if(remain_file_size > MAX_SIZE_COPY_FILE)
//	{
//		lv_fs_seek(&sd_file, offset); 
//		lv_fs_seek(&flash_file,  offset); 

//		ret = lv_fs_read(&sd_file, copy_buffer, MAX_SIZE_COPY_FILE, &fwr_rd_num);
//		if(fwr_rd_num != MAX_SIZE_COPY_FILE || ret != FR_OK)
//		{
//			HXLOG("read source file fail");
//			goto exit;
//		}
//		ret=lv_fs_write(&flash_file, copy_buffer, MAX_SIZE_COPY_FILE, &fwr_rd_num);
//		if(fwr_rd_num != MAX_SIZE_COPY_FILE || ret != FR_OK)
//		{
//			HXLOG("read source file fail");
//			goto exit;
//		}
//		remain_file_size -= MAX_SIZE_COPY_FILE;
//		offset += MAX_SIZE_COPY_FILE;
//		return ((offset * 100)/file_size) ;
//	}
//	else if(remain_file_size > 0)
//	{
//		lv_fs_seek(&sd_file, offset); 
//		lv_fs_seek(&flash_file,  offset); 

//		ret = lv_fs_read(&sd_file, copy_buffer, remain_file_size, &fwr_rd_num);
//		if(fwr_rd_num != remain_file_size || ret != FR_OK)
//		{
//			HXLOG("read source file fail");
//			goto exit;
//		}
//		ret=lv_fs_write(&flash_file, copy_buffer, remain_file_size, &fwr_rd_num);
//		if(fwr_rd_num != remain_file_size || ret != FR_OK)
//		{
//			HXLOG("read source file fail");
//			goto exit;
//		}
//		copy_status = 0;
//		lv_fs_close(&sd_file);
//		lv_fs_close(&flash_file);
//		return 100;
//	}
//	exit:
//	lv_fs_seek(&sd_file, 0);
//	lv_fs_close(&sd_file);
//	lv_fs_seek(&flash_file, 0);
//	lv_fs_close(&flash_file);
//	copy_status = 0;
//	return -1;
//}

///**
// * check sd card insert status
// * @return  0 - sd card has been inserted. ready into update file screen
// *         -1 - sd card undetected
// */
//int bsp_check_sd(void)
//{	
//	static FRESULT ret;
//	DWORD disk_size, free_size;
//	
//	// try to mount flash use fatfs
//    ret = f_mount(&flash_fs, "flash:", 1);
//	
//	FIL *fl = lv_mem_alloc(sizeof(FIL));
//	ret = f_open(fl,"flash:bg_1.bin", FA_OPEN_EXISTING | FA_READ);
//	//hal_spi_flash_erase(NULL, 0, 1);
//	// us should formatting the flash because of no FATFS
//	if ( ret == FR_NO_FILESYSTEM)
//    {
//		char *work = (char *)lv_mem_alloc(FF_MAX_SS);
//        if (!work) {
//            return -1;
//        }
//		MKFS_PARM opt;
//        disk_ioctl(DEV_SPI_FLASH, GET_FORMAT_OPTION, &opt);
//		ret = f_mkfs("flash:",(const MKFS_PARM*)&opt, work, FF_MAX_SS);
//		lv_mem_free(work);
//		if (ret != FR_OK) {
//           return -1;
//        }
//		HXLOG("flash format success.\r\n");
//        f_mount(NULL, "flash:", 1);
//        ret = f_mount(&flash_fs, "flash:", 1);

//        if (ret != FR_OK) {
//            return -1;
//        }
//    }
//	// us should mount sd card
//	if (f_mount(&sd_fs, "sd:", 1) != FR_OK)
//    {
//        HXLOG("SD Card mount error.\r\n");
////		if(GPIO_GetBit(GPIOB, PIN7) == 0)
////			NVIC_SystemReset();
//		return -1;
//    }
//	//get sd's totle size and free size, us used it in debug
//	exf_getfree("sd:", &disk_size, &free_size);
//	HXLOG("SD Card mount success.\r\n");
//	img_index = 0;
//	disp_blk_control(1);
//	//sd card inserted, start update file
//	setup_file_update_screen();
//	guiPage.updating = 1;
//	return 0;
//}

///* 
// *  img update form sd card
// *  the task created at setup_file_update_screen()
// */
//void lv_file_update_task(struct _lv_task_t *vags)
//{
//	int progress_value = 0;
//	progress_value = img_copy_sd_to_flash(gui_img_name[img_index], gui_img_name[img_index]);
//	if(progress_value < 0)
//	{
//		HXLOG("copy file fail");
//		return;
//	}
//	update_window_content_write((char*)gui_img_name[img_index], progress_value, 0);
//	if(progress_value == 100)
//	{
//		img_index++;
//		if(img_index >= MAX_IMG_NUM)
//		{
//			HXLOG("copy success");
//			distroy_file_update_screen();
//			guiPage.updating = 0;
//			return;
//		}
//		update_window_content_write((char*)gui_img_name[img_index], 0, 1);
//	}
//}

///*
// * img's file load to sram when mcu power on
//*/
//void img_loader(void)
//{
//	static uint8_t update_index = 0;
//	static uint32_t file_size;
//	static uint32_t start_addr = IMG_SDRAM_ADDR;
//	int ret;
//	
//	if(update_index >= MAX_IMG_NUM)return;
//	
//	// first load, init loader screen
//	if(update_index == 0)
//	{
//		guiPage.loading = 1;
//		UART_Send_end();
//		setup_scr_screen_wait();
//		
//	}
//	
//	ret = img_copy_flash_to_sdram(gui_img_name[update_index], start_addr, &file_size, &guiPage.page_img[update_index]);
//	if(ret < 0)
//	{
//		HXLOG("copy img from falsh fail");
//		return;
//	}
//	start_addr += file_size;
//	start_addr += start_addr%4;
//	update_index++;
//	
//	ico_screen_wait_update(update_index*100/MAX_IMG_NUM);
//	
//	// load complated
//	if(update_index >= MAX_IMG_NUM)
//	{
//		guiPage.loading = 0;
//	}
//}



